package ${appProperty.basePackage}.config;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
* @author ${appProperty.author}
* @version V1.0
* <p>Description: adb 配置</p>
* @date ${appProperty.dateTime}
*/
@Slf4j
@Configuration
@MapperScan(value = "${appProperty.basePackage}.dao", sqlSessionFactoryRef = "adbSqlSessionFactory")
public class AdbConfig {

    @Value("${r'$'}{adb.jdbc.driverClass}")
    private String driver;
    @Value("${r'$'}{adb.jdbc.url}")
    private String url;
    @Value("${r'$'}{adb.jdbc.username}")
    private String username;
    @Value("${r'$'}{adb.jdbc.password}")
    private String password;

    @Value("${r'$'}{druid.initialSize}")
    private int initialSize;
    @Value("${r'$'}{druid.maxActive}")
    private int maxActive;
    @Value("${r'$'}{druid.maxWait}")
    private int maxWait;
    @Value("${r'$'}{druid.minIdle}")
    private int minIdle;
    @Value("${r'$'}{druid.testOnBorrow}")
    private boolean testOnBorrow;

    @Primary
    @Bean(name = "adbDataSource")
    public DruidDataSource dataSource() {

        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);

        dataSource.setInitialSize(initialSize);
        dataSource.setMaxActive(maxActive);
        dataSource.setMinIdle(minIdle);
        dataSource.setMaxWait(maxWait);
        dataSource.setValidationQuery("SELECT 1");
        dataSource.setTestOnBorrow(testOnBorrow);

        return dataSource;
    }

    @Bean(name = "adbSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("adbDataSource") DataSource dataSource) throws Exception {

        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);

        // 添加xml配置文件目录
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        bean.setMapperLocations(resolver.getResources("classpath*:mapper/*.xml"));
        return bean.getObject();
    }

    @Bean("adbSqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("adbSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean("adbJdbcTemplate")
    public JdbcTemplate jdbcTemplate(@Qualifier("adbDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}
